﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Data.OleDb;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.MaintainScrollPositionOnPostBack = true;
        if (!IsPostBack)
        {
            ddlCat.SelectedIndex = -1;
        }
    }
    protected void btnAdvSearch_Click(object sender, EventArgs e)
    {
        if (btnAdvSearch.Text.Equals("+"))
        {
            btnAdvSearch.Text = "-";
            pnSearch.Visible = true;
        }
        else if (btnAdvSearch.Text.Equals("-"))
        {
            pnSearch.Visible = false;
            btnAdvSearch.Text = "+";
        }
    }

    protected void ddlCat_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlCat.SelectedIndex != 0)
        {
            AssetConfig assetConfig = new AssetConfig();
            assetGridView.DataSource = assetConfig.getAssetDataSet(ddlCat.SelectedValue);
            assetGridView.DataBind();
            txtSearchID.Text = "";
            txtValue.Text = "";

            ddlCriteria.Items.Clear();
            ddlCriteria.Items.AddRange(assetConfig.getCategoryAdvSearchDdl(ddlCat.SelectedValue));
            ddlCriteria.DataBind();
            showButtons();
        }
        else
        {
            assetGridView.DataSource = null;
            assetGridView.DataBind();
            txtSearchID.Text = "";
            txtValue.Text = "";
        }

    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        if (ddlCat.SelectedIndex != 0)
        {
            AssetConfig assetConfig = new AssetConfig();
            assetGridView.DataSource = assetConfig.getAssetDataSet(ddlCat.SelectedValue);
            assetGridView.DataBind();
            txtSearchID.Text = "";
        }
        else
        {
            assetGridView.DataSource = null;
            assetGridView.DataBind();
        }
    }

    protected void btnAdvSearchValue_Click(object sender, EventArgs e)
    {
        AssetConfig assetConfig = new AssetConfig();
        assetGridView.DataSource = assetConfig.getAssetDataSet_ByAdvSearch(ddlCat.SelectedValue, ddlCriteria.SelectedValue, txtValue.Text);
        assetGridView.DataBind();
        txtSearchID.Text = "";

    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        String searchID = txtSearchID.Text;
        if (IsNumeric(searchID))
        {
            int computer_ID = Int32.Parse(searchID);

            AssetConfig assetConfig = new AssetConfig();
            assetGridView.DataSource = assetConfig.getAssetDataSet(ddlCat.SelectedValue, computer_ID);
            assetGridView.DataBind();
        }
        else
        {
            assetGridView.DataSource = null;
            assetGridView.DataBind();
        }
    }

    protected void assetGridView_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        GridViewRow row = assetGridView.Rows[e.NewSelectedIndex];

        if (ddlCat.SelectedValue.Equals("Computers"))
        {
            if (ddlCriteria.SelectedValue.Equals("*Parts not used"))
            {
                int partID = Int32.Parse(row.Cells[1].Text);

                Session["Part_ID"] = partID;//used in the Parts.aspx page
                Session["hasComptuerID"] = false;//used in the Parts.aspx page
                Response.Redirect("Parts/Parts.aspx");
            }
            else
            {
                Session["Computer_ID"] = row.Cells[1].Text;
                Response.Redirect("Computers/Computers.aspx", true);
            }
        }
        else if(ddlCat.SelectedValue.Equals("Software"))
        {
            Session["Software_ID"] = row.Cells[1].Text;
            Response.Redirect("Software/Software.aspx", true);
        }
        else if (ddlCat.SelectedValue.Equals("Other"))
        {
            Session["Other_ID"] = row.Cells[1].Text;
            Response.Redirect("Other/Other.aspx", true);
        }
    }
    protected void assetGridView_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';this.style.color='Blue';";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';this.style.color='Black';";
            e.Row.ToolTip = "Click to select row";
            e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.assetGridView, "Select$" + e.Row.RowIndex);
        }
    }

    private static bool IsNumeric(object numberString)
    {
        char[] ca = numberString.ToString().ToCharArray();
        for (int i = 0; i < ca.Length; i++)
        {
            if (!char.IsNumber(ca[i]))
                if (ca[i] != '.')
                    return false;
        }
        if (numberString.ToString().Trim() == "")
            return false;
        return true;
    }

    private void showButtons()
    {
        txtSearchID.Visible = true;
        Button2.Visible = true;
        Button3.Visible = true;
        Label1.Visible = true;
        Label2.Visible = true;
        btnAdvSearch.Visible = true;

    }
}